package com.football.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.football.service.HttpService;
import com.football.service.NewsService;
import com.football.service.VideoService;
import com.football.util.JedisUtil;
import com.football.util.MD5;
import okhttp3.FormBody;
import top.hserver.core.interfaces.HttpRequest;
import top.hserver.core.interfaces.HttpResponse;
import top.hserver.core.ioc.annotation.*;
import top.hserver.core.server.util.JsonResult;

/**
 * api接口请求类
 */
@Controller
public class ApiController {

  @Autowired
  private HttpService httpService;

  @Autowired
  private VideoService videoService;

  @Autowired
  private NewsService newsService;


  @RequestMapping("/api")
  public String post(HttpRequest httpRequest, HttpResponse httpResponse) throws Exception {
    httpResponse.setHeader("Access-Control-Allow-Origin", "*");
    httpResponse.setHeader("Access-Control-Allow-Methods", "POST,GET,OPTIONS,DELETE"); // 允许请求的类型
    httpResponse.setHeader("Access-Control-Allow-Credentials", " true"); // 设置是否允许发送 cookies
    httpResponse.setHeader("Access-Control-Allow-Headers", " Content-Type,Content-Length,Accept-Encoding,X-Requested-with, Origin"); // 设置允许自定义请求头的字段'
    httpResponse.setHeader("content-type", "application/json; charset=utf-8");
    String rawData = httpRequest.getRawData();
    if (rawData.trim().length() > 0) {
      JSONObject jsonObject = JSON.parseObject(rawData);
      jsonObject.remove("timestamp");
      String md5 = "api:" + MD5.getMD5(JSON.toJSONString(jsonObject));
      String s = JedisUtil.get(md5);
      if (s != null) {
        return s;
      } else {
        String post = httpService.post(rawData);
        JedisUtil.setex(md5, 20, post);
        return post;
      }
    } else {
      return JSON.toJSONString(JsonResult.error());
    }
  }


  @RequestMapping("/video_api")
  public String video_api(HttpResponse httpResponse, HttpRequest httpRequest) throws Exception {
    httpResponse.setHeader("Access-Control-Allow-Origin", "*");
    httpResponse.setHeader("Access-Control-Allow-Methods", "POST,GET,OPTIONS,DELETE"); // 允许请求的类型
    httpResponse.setHeader("Access-Control-Allow-Credentials", " true"); // 设置是否允许发送 cookies
    httpResponse.setHeader("Access-Control-Allow-Headers", " Content-Type,Content-Length,Accept-Encoding,X-Requested-with, Origin"); // 设置允许自定义请求头的字段'
    httpResponse.setHeader("content-type", "application/json; charset=utf-8");
    String page = httpRequest.query("page");
    String md5 = "video_api:" + page;
    String s = JedisUtil.get(md5);
    if (s == null) {
      FormBody formBody = new FormBody.Builder()
        .add("channel_id", "2")
        .add("sub_channel_id", "11")
        .add("type", "2")
        .add("page", page)
        .build();
      String post = videoService.post(formBody);
      JedisUtil.setex(md5, 60 * 30, post);
      return post;
    } else {
      return s;
    }
  }


  @RequestMapping("/article_api")
  public String article_api(HttpResponse httpResponse, HttpRequest request) throws Exception {
    httpResponse.setHeader("Access-Control-Allow-Origin", "*");
    httpResponse.setHeader("Access-Control-Allow-Methods", "POST,GET,OPTIONS,DELETE"); // 允许请求的类型
    httpResponse.setHeader("Access-Control-Allow-Credentials", " true"); // 设置是否允许发送 cookies
    httpResponse.setHeader("Access-Control-Allow-Headers", " Content-Type,Content-Length,Accept-Encoding,X-Requested-with, Origin"); // 设置允许自定义请求头的字段'
    httpResponse.setHeader("content-type", "application/json; charset=utf-8");

    String page = request.query("page");
    String md5 = "article_api:" + page;
    String s = JedisUtil.get(md5);
    if (s != null) {
      return s;
    } else {
      FormBody formBody = new FormBody.Builder()
        .add("channel_id", "26")
        .add("sub_channel_id", "0")
        .add("type", "9")
        .add("page", page)
        .build();
      String s1 = newsService.postNews(formBody);
      JedisUtil.setex(md5, 60 * 30, s1);
      return s1;
    }
  }

  @RequestMapping("/newsDetails_api")
  public String newsDetails_api(HttpResponse httpResponse, HttpRequest request) throws Exception {
    httpResponse.setHeader("Access-Control-Allow-Origin", "*");
    httpResponse.setHeader("Access-Control-Allow-Methods", "POST,GET,OPTIONS,DELETE"); // 允许请求的类型
    httpResponse.setHeader("Access-Control-Allow-Credentials", " true"); // 设置是否允许发送 cookies
    httpResponse.setHeader("Access-Control-Allow-Headers", " Content-Type,Content-Length,Accept-Encoding,X-Requested-with, Origin"); // 设置允许自定义请求头的字段'
    httpResponse.setHeader("content-type", "application/json; charset=utf-8");
    String news_id = request.query("news_id");
    String md5 = "newsDetails_api:" + news_id;
    String s = JedisUtil.get(md5);
    if (s != null) {
      return s;
    } else {
      FormBody formBody = new FormBody.Builder()
        .add("news_id", news_id)
        .add("appkey", "a013bae0519701be424bb6bd2548acd5")
        .build();
      String s1 = newsService.postDetail(formBody);
      JedisUtil.setex(md5, 60 * 60 * 24, s1);
      return s1;
    }
  }
}
